package com.xcf.controller.business.common;
import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.xcf.controller.base.BaseController;
import com.xcf.entity.Page;
import com.xcf.service.business.common.shipowner.ShipOwnerService;
import com.xcf.util.PageData;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


@Controller
@RequestMapping(value="/ship")
public class ShipOwnerController  extends BaseController{
	@Resource
	private ShipOwnerService shipownerservice;
	String menuUrl="ship/owner";//菜单地址(权限用) 
	@RequestMapping(value="/owner")
	public ModelAndView ship(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
	
		try {
			pd=this.getPageData();
			String OWNER_CODE = pd.getString("OWNER_CODE");  //判断箱主代码
			if (null != OWNER_CODE && !"".equals(OWNER_CODE)) { 
				pd.put("OWNER_CODE", OWNER_CODE.trim());
			}
			String ENAME = pd.getString("ENAME");  //判断英文名称
			if (null != ENAME && !"".equals(ENAME)) {
				pd.put("ENAME", ENAME.trim());
			}
			String OWNER_NAME = pd.getString("OWNER_NAME"); //判断箱主名称
			if (null != OWNER_NAME && !"".equals(OWNER_NAME)) {
				pd.put("OWNER_NAME", OWNER_NAME.trim());
			}
			String SHORT_NAME = pd.getString("SHORT_NAME"); //判断箱主简称
			if (null != SHORT_NAME && !"".equals(SHORT_NAME)) {
				pd.put("SHORT_NAME", SHORT_NAME.trim());
			}
			String INDEX_CODE = pd.getString("INDEX_CODE"); //判断检索码
			if (null != INDEX_CODE && !"".equals(INDEX_CODE)) {
				pd.put("INDEX_CODE", INDEX_CODE.trim());
			}
			page.setPd(pd);
			List<PageData> list = shipownerservice.seach(page);
			mv.addObject("list", list);
			mv.addObject("pd", pd);
			mv.setViewName("/business/common/shipowner/shipOwner");
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		return mv;
		
		
	}
	
	             //点添加新增一条
			@RequestMapping(value="/addship")
			@ResponseBody
			public String addship(){
				PageData pd = new PageData();
				    pd = this.getPageData();
					try {
						if(pd.getString("ID")==null || pd.getString("ID")==""){
						shipownerservice.addship(pd);				
					}else{
						shipownerservice.updateship(pd);}
					
					} catch (Exception e) {				
					
						e.printStackTrace();
					}
					return null;

			}
			//修改页面
			@RequestMapping(value="/editship")
			public ModelAndView shipedit(){
			ModelAndView mv = this.getModelAndView();
			  try {
				  PageData pd = new PageData();
				  pd = this.getPageData();
				  pd= shipownerservice.seacById(pd);
				  mv.addObject("pd", pd);
				  mv.setViewName("/business/common/shipowner/shipowner_edit");
			} catch (Exception e) {
				e.printStackTrace();
			}
			  return mv;
			}
			//删除
			@RequestMapping(value="deleteU")
			@ResponseBody
			public String deleteU(){
				PageData pd = new PageData();
				pd = this.getPageData();
				try {
					shipownerservice.delete(pd);
					
				} catch (Exception e) {
					
					e.printStackTrace();
				}
				return null;
					
	}

			//批量删除
			@RequestMapping(value="/deleteInfo")
			@ResponseBody
			public String deleteinfo(){
				PageData pd = new PageData();
				pd = this.getPageData();
					try {
						
						String data = pd.getString("dataJsonStr");

						JSONArray items = JSONArray.fromObject(data);
						for(int i=0,size=items.size();i<size;i++){
							JSONObject item = items.getJSONObject(i);
							String id = item.getString("ID").trim();
							PageData pdq = new PageData();
							pdq.put("ID", id);
							shipownerservice.delete(pdq);
						}
					} catch (Exception e) {
						
						e.printStackTrace();
					}
				
				return null;
				

				
			}

}
